﻿using MES_SystemUI.Domain;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MES_SystemUI.UI
{
    public partial class StoresPutin : MES_SystemUI.UI.Putin
    {
        public StoresPutin()
        {
            InitializeComponent();
            this.Load += StoresPutin_Load;
            textBox3.Click += TextBox3_Click;
            comboBox2.SelectionChangeCommitted += ComboBox2_SelectionChangeCommitted;
        }

        private void ComboBox2_SelectionChangeCommitted(object sender, EventArgs e)
        {
            if (comboBox2.SelectedIndex != -1)
            {
                using (var c = new Context.MES_SystemContext())
                {
                    textBox2.Text = c.users.Find(comboBox2.SelectedValue).Phone;
                }
            }
        }

        private void StoresPutin_Load(object sender, EventArgs e)
        {
            ComboBoxBingData();
            RefreshText();
        }

        private void RefreshText()
        {
            comboBox1.SelectedIndex = -1;
            comboBox2.SelectedIndex = -1;
            comboBox1.Text = string.Empty;
            comboBox2.Text = string.Empty;
            textBox2.Enabled = false;
            textBox2.Text = string.Empty;
            textBox3.Text = string.Empty;
            textBox4.Text = string.Empty;
            textBox5.Text = string.Empty;
        }

        private void ComboBoxBingData()
        {
            using (var c = new Context.MES_SystemContext())
            {
                comboBox1.DataSource = c.storeCategories.ToList();
                comboBox1.DisplayMember = "StoreCategorieName";
                comboBox1.ValueMember = "StoreCategorieId";
                comboBox2.DataSource = c.users.ToList();
                comboBox2.DisplayMember = "UserName";
                comboBox2.ValueMember = "UserId";
            }
        }

        private void TextBox3_Click(object sender, EventArgs e)
        {
            textBox3.Text = "CK" + DateTime.Now.ToString("yyyyMMddHHmmss");
        }
        public override void PutinData()
        {
            if (comboBox1.SelectedIndex == -1)
            {
                MessageBox.Show("仓库类型不能为空");
                return;
            }
            if (comboBox2.SelectedIndex == -1)
            {
                MessageBox.Show("仓管员不能为空");
                return;
            }
            if (string.IsNullOrEmpty(textBox3.Text.Trim()))
            {
                MessageBox.Show("仓库编号不能为空");
                return;
            }
            if (string.IsNullOrEmpty(textBox4.Text.Trim()))
            {
                MessageBox.Show("仓库名称不能为空");
                return;
            }
            if (string.IsNullOrEmpty(textBox5.Text.Trim()))
            {
                MessageBox.Show("地址不能为空");
                return;
            }
            using (var c = new Context.MES_SystemContext())
            {
                Store mode = new Store()
                {
                    StoreCode = textBox3.Text,
                    StoreName = textBox4.Text,
                    StoreAddress = textBox5.Text,
                    StoreCategoryId = int.Parse(comboBox1.SelectedValue.ToString()),
                    Storekeeper = int.Parse(comboBox2.SelectedValue.ToString()),
                    CreateUserId = SystemDataConfig.LoginUser.UserId,
                };
                c.Entry(mode).State = System.Data.Entity.EntityState.Added;
                c.SaveChanges();
            }
            RefreshText();
            MessageBox.Show("提交成功！");
        }
    }
}
